package cn.cfoodc.sharding.controller;

import cn.cfoodc.sharding.domain.TConfig;
import cn.cfoodc.sharding.service.TConfigService;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试广播表
 */
@RestController
public class DemoConfig {

    private TConfigService tConfigService;
    private SnowflakeKeyGenerateAlgorithm NEXT_ID ;
    @Autowired
    public DemoConfig(TConfigService tConfigService,SnowflakeKeyGenerateAlgorithm snowflakeKeyGenerateAlgorithm){
        this.tConfigService=tConfigService;
        this.NEXT_ID=snowflakeKeyGenerateAlgorithm;
    }

    @GetMapping("/selectConfigAll")
    public List<TConfig> selectAll(){

        return tConfigService.selectAll();
    }

    @PostMapping("/insertConfigAndArgs")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE，这里只引入了XA
    public int insertConfigAndArgs(@RequestParam("name") String name , @RequestParam("value") String value){
        TConfig tConfig = new TConfig((Long) NEXT_ID.generateKey(),name,value);
        return tConfigService.insert(tConfig);
    }
}
